trigger AddSharePeople on JoinPeople__c (after insert,after delete) {
    if(system.trigger.isinsert){
        List<JoinPeople__c> jpList = new List<JoinPeople__c>();
        List<ID> idList = new List<ID>();
        List<CityNameList__c> cnList = new List<CityNameList__c>();
        List<CityNameList__Share> ccList = new List<CityNameList__Share>();
        
        List<HCPEducation__c> hcpeList = new List<HCPEducation__c>();
        List<HCPEducation__Share> hcpsList = new List<HCPEducation__Share>();
        
        List<HCPEducation__Share> nhcpsList = new List<HCPEducation__Share>();
        
        List<ConsumerEducation__c> csList = new List<ConsumerEducation__c>();
        List<ConsumerEducation__Share> cssList = new List<ConsumerEducation__Share>();
        
        boolean cFlag = false;
        boolean sFlag = false;
        boolean uFlag = false; //set for test
        
        for (JoinPeople__c  jp: Trigger.new)
        {
            jpList.add(jp);
            if(jp.CityNameList__c != null){
                idList.add(jp.CityNameList__c);
                cFlag = true;
            }
            if(jp.LocalCSEventPlan__c != null){
                idList.add(jp.LocalCSEventPlan__c);
                sFlag = true;
            }
            if(jp.NationalHCPEducation__c != null)
            {
                idList.add(jp.NationalHCPEducation__c);
                //set for test
                uFlag = true;
                            
            }
        }
        if(idList.size() > 0)
        {
            if(cFlag == true){
                cnList = [Select id,name,NationalHCPID__c From CityNameList__c Where id in: idList];
                for(CityNameList__c cn: cnList)
                {
                    
                    for(JoinPeople__c  jp: jpList)
                    {
                        if(jp.CityNameList__c == cn.id){
                            CityNameList__Share csShare = new CityNameList__Share();
                            csShare.ParentId = cn.id;
                            csShare.UserOrGroupId = jp.User__c;
                            csShare.AccessLevel = 'Edit';
                            ccList.add(csShare);
                            
                            HCPEducation__Share nhcpShare = new HCPEducation__Share();
                            nhcpShare.ParentId = (ID)cn.NationalHCPID__c;
                            nhcpShare.UserOrGroupId = jp.User__c;
                            nhcpShare.AccessLevel = 'Edit';
                            nhcpsList.add(nhcpShare);
                        }
                     }
                }
                insert ccList;
                insert nhcpsList;        
                
            }
            if(uFlag == true){
                hcpeList = [Select id,name From HCPEducation__c Where id in: idList];
                for(HCPEducation__c cn: hcpeList)
                {
                    for(JoinPeople__c  jp: jpList)
                    {
                        if(jp.NationalHCPEducation__c == cn.id){
                            HCPEducation__Share csShare = new HCPEducation__Share();
                            csShare.ParentId = cn.id;
                            csShare.UserOrGroupId = jp.User__c;
                            csShare.AccessLevel = 'Edit';
                            hcpsList .add(csShare);                   
                        }
                     }
                }
                insert hcpsList ;        
            }
            if(sFlag == true){
                csList = [Select id,name From ConsumerEducation__c Where id in: idList];
                for(ConsumerEducation__c cn: csList )
                {
                    for(JoinPeople__c  jp: jpList)
                    {
                        if(jp.LocalCSEventPlan__c == cn.id){
                            ConsumerEducation__Share csShare = new ConsumerEducation__Share();
                            csShare.ParentId = cn.id;
                            csShare.UserOrGroupId = jp.User__c;
                            csShare.AccessLevel = 'Read';
                            cssList.add(csShare);                   
                        }
                     }
                }
                insert cssList;        
            }
        }
    }
    if(system.trigger.isdelete){
        HCPEducation__Share DsaloonList = new HCPEducation__Share();      
        HCPEducation__Share DnhcpsList = new HCPEducation__Share();        
        ConsumerEducation__Share DcssList = new ConsumerEducation__Share();
        CityNameList__c Dcn = new CityNameList__c();
        
        JoinPeople__c delJP = Trigger.old[0];
        if(delJP.CityNameList__c != null){
            Dcn = [Select id,name,NationalHCPID__c From CityNameList__c Where id =: delJP.CityNameList__c];
            DnhcpsList = [Select id From HCPEducation__Share where ParentId =: Dcn.NationalHCPID__c and UserOrGroupId =: delJP.ownerid];
            delete DnhcpsList;
        }
        if(delJP.LocalCSEventPlan__c != null){
            DcssList = [Select id From ConsumerEducation__Share where ParentId =: delJP.LocalCSEventPlan__c and UserOrGroupId =: delJP.ownerid];
            delete DcssList;
        }
        if(delJP.NationalHCPEducation__c != null)
        {
            DsaloonList = [Select id From HCPEducation__Share where ParentId =: delJP.NationalHCPEducation__c and UserOrGroupId =: delJP.ownerid];
            delete DsaloonList;
                        
        }
    }    
}